


import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Iterator;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

public class EmployeeFrame extends JFrame 
{

	private JPanel contentPane;
	private JMenuBar menuBar;
	private JMenu mnFile;
	private JMenuItem mntmOpen;
	private JMenuItem mntmSave;
	private JMenuItem mntmSaveAsAnother;
	private JMenuItem mntmQuit;
	private JMenu mnManagement;
	private JMenuItem mntmSearch;
	private JMenuItem mntmInsert;
	private JMenuItem mntmUpdateDelete;
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;
	
	private FileInputStream fis 	= null;
	private InputStreamReader isr 	= null;
	private BufferedReader br 		= null;
	private JFileChooser jc 		= null;
	private FileOutputStream fos 	= null;
	private OutputStreamWriter osw 	= null;
	private BufferedWriter bw		= null;
	private String fileName 		= "Management.txt";
	
	public static HashMap<Integer, MemberBean> map = new HashMap<Integer, MemberBean>();
	private JMenu mnDepartment;
	private JMenuItem mntmNewMenuItem;
	private JMenuItem mntmNewMenuItem_1;
	private JMenuItem mntmNewMenuItem_2;
	
	

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) 
	{
		EventQueue.invokeLater(new Runnable() 
		{
			public void run()
			{
				try 
				{
					EmployeeFrame frame = new EmployeeFrame();
					frame.setVisible(true);
				} catch (Exception e)
				{
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public EmployeeFrame() 
	{
		setBackground(Color.WHITE);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 640, 480);
		setJMenuBar(getMenuBar_1());
		contentPane = new JPanel();
		contentPane.setBackground(new Color(245, 255, 250));
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
	}

	private JMenuBar getMenuBar_1()
	{
		if (menuBar == null) {
			menuBar = new JMenuBar();
			menuBar.setAlignmentX(Component.RIGHT_ALIGNMENT);
			menuBar.setFont(new Font("돋움", Font.BOLD, 12));
			menuBar.setBackground(SystemColor.inactiveCaption);
			menuBar.add(getMnFile());
			menuBar.add(getMnManagement());
			menuBar.add(getMnDepartment());
		}
		return menuBar;
	}
	
	private JMenu getMnFile() 
	{
		if (mnFile == null) 
		{
			mnFile = new JMenu("File");
			mnFile.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mnFile.setBackground(SystemColor.textHighlight);
			mnFile.add(getMntmOpen());
			mnFile.add(getMntmSave());
			mnFile.add(getMntmSaveAsAnother());
			mnFile.add(getMntmQuit());
		}
		return mnFile;
	}
	
	private JMenuItem getMntmOpen() 
	{
		if (mntmOpen == null)
		{
			mntmOpen = new JMenuItem("Open");
			mntmOpen.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mntmOpen.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					
				}
			});
		}
		return mntmOpen;
	}
	
	private JMenuItem getMntmSave() 
	{
		if (mntmSave == null)
		{
			mntmSave = new JMenuItem("Save");
			mntmSave.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mntmSave.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					fileSave( "저장" );
				}
			});
		}
		return mntmSave;
	}
	
	private JMenuItem getMntmSaveAsAnother() 
	{
		if (mntmSaveAsAnother == null) 
		{
			mntmSaveAsAnother = new JMenuItem("Save as another...");
			mntmSaveAsAnother.setFont(new Font("Meiryo", Font.BOLD, 12));
			mntmSaveAsAnother.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					jc = new JFileChooser(fileName);
					int yn = jc.showSaveDialog( EmployeeFrame.this );
					
					if( yn == JFileChooser.APPROVE_OPTION ) {
						fileName = jc.getSelectedFile().getPath();
						fileSave( "다른 이름으로 저장" );
					}
				}
			});
		}
		return mntmSaveAsAnother;
	}
	
	private JMenuItem getMntmQuit() 
	{
		if (mntmQuit == null)
		{
			mntmQuit = new JMenuItem("Quit");
			mntmQuit.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mntmQuit.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					EmployeeFrame.this.dispose();
				}
			});
		}
		return mntmQuit;
	}
	
	private JMenu getMnManagement() 
	{
		if (mnManagement == null) 
		{
			mnManagement = new JMenu("Management");
			mnManagement.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mnManagement.setAlignmentX(Component.LEFT_ALIGNMENT);
			mnManagement.setBackground(SystemColor.textHighlight);
			mnManagement.add(getMntmSearch());
			mnManagement.add(getMntmInsert());
			mnManagement.add(getMntmUpdateDelete());
		}
		return mnManagement;
	}
	
	private JMenuItem getMntmSearch()
	{
		if (mntmSearch == null)
		{
			mntmSearch = new JMenuItem("Search");
			mntmSearch.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mntmSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					contentPane.removeAll();
					contentPane.add(new EmpSearchPanel());
					contentPane.validate();
				}
			});
		}
		return mntmSearch;
	}
	
	private JMenuItem getMntmInsert()
	{
		if (mntmInsert == null) 
		{
			mntmInsert = new JMenuItem("Insert");
			mntmInsert.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mntmInsert.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					contentPane.removeAll();
					contentPane.add(new EmpInsertPanel());
					contentPane.validate();
										
				}
			});
		}
		return mntmInsert;
	}
	
	private JMenuItem getMntmUpdateDelete() 
	{
		if (mntmUpdateDelete == null) 
		{
			mntmUpdateDelete = new JMenuItem("Update & Delete");
			mntmUpdateDelete.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mntmUpdateDelete.addActionListener(new ActionListener() 
			{
				public void actionPerformed(ActionEvent arg0) 
				{
				contentPane.removeAll();
				contentPane.add(new EmpModifyPanel());
				contentPane.validate();
				}
			});
		}
		return mntmUpdateDelete;
	}
	
	private void fileSave( String titleMsg) {
		try {
			fos = new FileOutputStream(fileName);
			osw = new OutputStreamWriter( fos );
			bw 	= new BufferedWriter( osw );
			
			Iterator<MemberBean> itValue = EmployeeFrame.map.values().iterator();
			
			while( itValue.hasNext() ) {
				MemberBean m = itValue.next();
				
				bw.write( m.getEmpId() + ", " + m.getFirName() + ", " + m.getLastName() + ", " + 
						m.getDeptName() + ", " + m.getEmail() + ", " + 
						m.getpNum() + ", " + m.getMngId() + ", " + 
						m.getZipcode() + ", " + m.getAddr() + "\n");
			}
			
			bw.close();
			osw.close();
			fos.close();
			
			JOptionPane.showMessageDialog( null, "저장이 완료 되었습니다.", titleMsg, 
					JOptionPane.INFORMATION_MESSAGE );
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private JMenu getMnDepartment() {
		if (mnDepartment == null) {
			mnDepartment = new JMenu("Department");
			mnDepartment.setFont(new Font("Meiryo UI", Font.BOLD, 12));
			mnDepartment.add(getMntmNewMenuItem());
			mnDepartment.add(getMntmNewMenuItem_1());
			mnDepartment.add(getMntmNewMenuItem_2());
		}
		return mnDepartment;
	}
	private JMenuItem getMntmNewMenuItem() {
		if (mntmNewMenuItem == null) {
			mntmNewMenuItem = new JMenuItem("Search");
			mntmNewMenuItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
					contentPane.removeAll();
					contentPane.add(new DeptSearchPanel());
					contentPane.validate();
					
				}
			});
			mntmNewMenuItem.setFont(new Font("Meiryo UI", Font.BOLD, 12));
		}
		return mntmNewMenuItem;
	}
	private JMenuItem getMntmNewMenuItem_1() {
		if (mntmNewMenuItem_1 == null) {
			mntmNewMenuItem_1 = new JMenuItem("Insert");
			mntmNewMenuItem_1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					contentPane.removeAll();
					contentPane.add(new DeptInsertPanel());
					contentPane.validate();
				
				}
			});
			mntmNewMenuItem_1.setFont(new Font("Meiryo UI", Font.BOLD, 12));
		}
		return mntmNewMenuItem_1;
	}
	private JMenuItem getMntmNewMenuItem_2() {
		if (mntmNewMenuItem_2 == null) {
			mntmNewMenuItem_2 = new JMenuItem("Update & Delete");
			mntmNewMenuItem_2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					contentPane.removeAll();
					contentPane.add(new DeptModifyPanel());
					contentPane.validate();
				}
			});
			mntmNewMenuItem_2.setFont(new Font("Meiryo UI", Font.BOLD, 12));
		}
		return mntmNewMenuItem_2;
	}
}
